package controller;

import dao.RoleDao;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class RoleServlet extends HttpServlet {
    private RoleDao roleDao;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String operateType = request.getParameter("operateType");
        roleDao = new RoleDao();
        switch (operateType){
            case "add":
                addRole(request,response);
                break;
            case "delete":
                deleteRole(request,response);
                break;
            case "alter":
                alterRole(request,response);
                break;
            case "showDetail":
                showRoleDetail(request,response);
                break;
            case "showPaged":
                showPagedRole(request,response);
                break;
            case "showAll":
                showAllRole(request,response);
                break;
            default:
                break;
        }

    }

    private void showAllRole(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Object> roles = roleDao.showAllRole();
        JSONObject result = new JSONObject();
        JSONObject content = new JSONObject();
        content.put("roles", JSONArray.fromObject(roles));
        result.put("result", content);
        response.getWriter().print(result.toString());
    }

    private void showPagedRole(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer onePageCount = Integer.parseInt(request.getParameter("onePageCount"));
        Integer currentPage = Integer.parseInt(request.getParameter("currentPage"));
        List<Object> roles = roleDao.showPagedRole(onePageCount, currentPage);
        int pageNum = roleDao.rolePageNum(onePageCount);
        JSONObject result = new JSONObject();
        JSONObject content = new JSONObject();
        content.put("roles", JSONArray.fromObject(roles));
        content.put("pageNum", pageNum);
        result.put("result", content);
        response.getWriter().print(result.toString());
    }

    private void showRoleDetail(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer roleId = Integer.parseInt(request.getParameter("roleId"));
        JSONObject role = roleDao.showRoleDetail(roleId);
        JSONObject result = new JSONObject();
        JSONObject content = new JSONObject();
        content.put("role", role);
        result.put("result", content);
        response.getWriter().print(result.toString());
    }

    private void addRole(HttpServletRequest request, HttpServletResponse response) throws NullPointerException, IOException {
        String name = request.getParameter("name");
        String powerCode = request.getParameter("power_code");
        String introduction = request.getParameter("introduction");
        boolean isSuccess = roleDao.addRole(name, powerCode, introduction);
        JSONObject result = new JSONObject();
        JSONObject content = new JSONObject();
        content.put("success", isSuccess);
        result.put("result", content);
        response.getWriter().print(result.toString());

    }

    private void deleteRole(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        boolean isSuccess = roleDao.deleteRole(id);
        JSONObject result = new JSONObject();
        JSONObject content = new JSONObject();
        content.put("success", isSuccess);
        result.put("result", content);
        response.getWriter().print(result.toString());
    }

    private void alterRole(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        String name = request.getParameter("name");
        String powerCode = request.getParameter("power_code");
        String introduction = request.getParameter("introduction");
        boolean isSuccess = roleDao.alterRole(id, name, powerCode, introduction);
        JSONObject result = new JSONObject();
        JSONObject content = new JSONObject();
        content.put("success", isSuccess);
        result.put("result", content);
        response.getWriter().print(result.toString());
    }

}
